package com.weiyi.mashangzhu.Landlord.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Adapter.AccountAdapter;
import com.weiyi.mashangzhu.Landlord.Bean.AccountBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

;

public class SetAliAccountActivity extends BaseActivity {
    @BindView(R.id.lv_aliaccount)
    ListView lvAliaccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_ali_account);
        ButterKnife.bind(this);
        initAccount();

    }

    private void initAccount() {
        OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "")
                .url(API.BaseApi_L + API.GetAccount_Api).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                T.showShort(SetAliAccountActivity.this, "网络连接失败，请检查一下网络设置");
            }

            @Override
            public void onResponse(String response) {
                Log.e("bbbbbbb","==="+response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("status") == 0) {
                        final AccountBean accountBean = new Gson().fromJson(response, AccountBean.class);
                        if (accountBean.getData().size()!=0){
                            lvAliaccount.setAdapter(new AccountAdapter(accountBean.getData(),SetAliAccountActivity.this));
                            lvAliaccount.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent=new Intent();
                                    intent.putExtra("account",accountBean.getData().get(position).getAlipayAccount());
                                    setResult(0x002,intent);
                                    finish();
                                }
                            });
                        }


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initAccount();
    }


    @OnClick({R.id.back, R.id.add1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add1:
                UiUtil.startActivity(this, AddAliAccountActivity.class);
                break;
        }
    }
}
